package com.sys.web.async;

import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sys.dao.impl.UploadDaoImpl;

import net.sf.json.JSONObject;

@WebServlet("/UploadOpeAsy")
public class UploadOpeAsy extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String operation = request.getParameter("operation");
		if(operation.equals("delete")) {
			try {
				delete(request, response);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	private void delete(HttpServletRequest request, HttpServletResponse response)
			throws SQLException, IOException {
		String userId = (String) request.getSession().getAttribute("user");
		//用户为空时，不能访问“我的主页”，跳转至主页面
		if(userId==null) {
			response.sendRedirect(request.getContextPath()+"\\homeUI");
		}
		String fileId = request.getParameter("fileId");
		Boolean b = new UploadDaoImpl().deleteRecord(userId+fileId);
		response.setContentType("text/html;charset=UTF-8");
		Map<String, Object> map = new HashMap<>();
		map.put("isSucess", b);
		JSONObject jsonObject = JSONObject.fromObject(map);
		response.getWriter().write(jsonObject.toString());
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}